package com.start.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.reggie.entity.Category;
import com.start.reggie.entity.Dish;
import com.start.reggie.entity.Setmeal;
import com.start.reggie.mapper.CategoryMapper;
import com.start.reggie.mapper.DishMapper;
import com.start.reggie.mapper.SetmealMapper;
import com.start.reggie.service.CategoryService;
import com.start.reggie.service.DishService;
import com.start.reggie.service.SetmealService;
import com.start.reggie.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * 根据id删除分类，删除之前需要进行判断
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void remove(Long id) {

        //查询当前分类是否关联了菜品，若已关联，则抛出异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (count1 != 0){
            //已经关联菜品，抛出一个异常
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        //查询当前分类是否关联了套餐，若已关联，则抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (count2 != 0){
            //已经关联套餐，抛出一个异常
            throw new RuntimeException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        categoryMapper.deleteById(id);

    }

}
